package com.etooling.pdm.repository;

import com.etooling.framework.repository.BaseRepository;
import com.etooling.pdm.entity.GoodsSpecs;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * @author dyz
 * @date 2023/4/12 10:37
 * @description GoodsSpecsRepository
 */
@Repository
public interface GoodsSpecsRepository extends BaseRepository<GoodsSpecs> {

    @Query(value = "SELECT * FROM pdm.t_goods_specs WHERE id = :id FOR UPDATE", nativeQuery = true)
    Optional<GoodsSpecs> findByIdWithLock(@Param("id") String id);

    /**
     * 查找参数List中,哪些内容是在指定companyId的数据中是存在的
     *
     * @param skuCodeList
     * @param companyId
     * @return
     */
    @Query(nativeQuery = true, value = "select sku_code from pdm.t_goods_specs where sku_code in ?1 and company_id = ?2 and status = ?3 ")
    List<String> skuCodeExistList(List<String> skuCodeList, String companyId, String status);


    @Query(nativeQuery = true, value = "select count(*) from pdm.t_goods_specs gs  " +
            "                left join pdm.t_goods g on gs.goods_id = g.id " +
            "                left join pdm.t_goods_type gt on g.goods_type_id = gt.id " +
            "                where gs.sku_code=?1 and gs.company_id=?2 and gs.status !=?3 and gt.is_product=?4 ")
    Integer countSkuCode(String skuCode, String companyId, String neStatus, Boolean isProduct);


}


